package drds.global_transaction.api.message_codec.message_codec;

import java.io.Serializable;
import java.nio.charset.Charset;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;

public abstract class Message implements IMessageCodec, Serializable
{// the full name is Message
	//
	public static final short register_transaction_manager_request_message = 1;
	public static final short register_transaction_manager_response_message = 2;
	//
	public static final short register_resource_manager_request_message = 3;
	public static final short register_resource_manager_response_message = 4;
	//
	public static final short global_transaction_begin_request_message = 11;
	public static final short global_transaction_begin_response_message = 12;
	//
	public static final short global_transaction_commit_request_message = 13;
	public static final short global_transaction_commit_response_message = 14;
	//
	public static final short global_transaction_rollback_request_message = 15;
	public static final short global_transaction_rollback_response_message = 16;
	//
	public static final short global_transaction_status_report_request_message = 17;
	public static final short global_transaction_status_report_response_message = 18;
	//
	public static final short global_transaction_lock_record_ids_check_request_message = 19;
	public static final short global_transaction_lock_record_ids_check_response_message = 20;
	//
	//
	public static final short branch_transaction_register_request_message = 51;
	public static final short branch_transaction_register_response_message = 52;
	//
	public static final short branch_transaction_commit_request_message = 53;
	public static final short branch_transaction_commit_response_message = 54;
	//
	public static final short branch_transaction_rollback_request_message = 55;
	public static final short branch_transaction_rollback_response_message = 56;
	//
	public static final short branch_transaction_status_report_request_message = 57;
	public static final short branch_transaction_status_report_response_message = 58;
	//
	//
	public static final short type_merge_warp = 101;
	public static final short type_merge_response = 102;

	//
	protected static final Charset UTF8 = Charset.forName("utf-8");
	private static final long serialVersionUID = 1l;
	protected ChannelHandlerContext channelHandlerContext;

	public static int bytesToInt(byte[] bytes, int offset)
	{
		int ret = 0;
		for (int i = 0; i < 4 && i + offset < bytes.length; i++)
		{
			ret <<= 8;
			ret |= (int) bytes[i + offset] & 0xFF;
		}
		return ret;
	}

	public static void intToBytes(int $int, byte[] bytes, int offset)
	{
		bytes[offset] = (byte) (($int >> 24) & 0xFF);
		bytes[offset + 1] = (byte) (($int >> 16) & 0xFF);
		bytes[offset + 2] = (byte) (($int >> 8) & 0xFF);
		bytes[offset + 3] = (byte) ($int & 0xFF);
	}

	@Override
	public boolean decode(ByteBuf byteBuf)
	{
		return false;
	}
}
